<div id="tb<?php echo $uniqid;?>" style="padding:5px">
    <table cellspacing="0" cellpadding="0">
        <tbody>
        <tr>
            <td><a href="javascript:void(0);" onclick="decorateAdd();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-tianjia1"></i><span
                    class="l-btn-text">增加</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:void(0);" onclick="decorateEdit();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-bianji"></i><span
                    class="l-btn-text">编辑</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:void(0);" onclick="decorateReload();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-shuaxin"></i><span
                    class="l-btn-text">重载</span></span></a></td>
            <td>&nbsp;&nbsp;<input id="decorateKeywords" style="width:100px" type="text" class="easyui-input textbox">
            </td>
            <td><a href="javascript:void(0);" onclick="decorateSearch();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-sousuo"></i><span
                    class="l-btn-text">查询</span></span></a></td>
        </tr>
        </tbody>
    </table>
</div>
<div class="con" style="-moz-user-select:none;">
    <table id="decorate"></table>
</div>
<div id="decorateDlg" class="easyui-dialog" style="width:720px; height:450px;" title="信息框" closed="true"></div>

<script>
    var decorateAdd = function () {
        $("#decorateDlg").dialog({
            title: '添加模板',
            closed: false,
            width: 720,
            height: 450,
            cache: false,
            href: '/admin/decorate/add?type=shop',
            modal: true,
            buttons: [{
                text: '保存',
                id: 'decoratesubmit',
                iconCls: 'icon-ok',
            }, {
                text: '关闭',
                iconCls: 'icon-no',
                handler: function () {
                    $("#decorateDlg").dialog('close');
                }
            }]
        });
    };
    var decorateEdit = function (id) {
        let rowId = id || ($('#decorate').treegrid('getSelected') && $('#decorate').treegrid('getSelected').id);
        if (rowId) {
            $("#decorateDlg").dialog({
                title: '编辑模板',
                closed: false,
                width: 720,
                height: 450,
                cache: false,
                href: '/admin/decorate/edit?id=' + rowId,
                modal: true,
                buttons: [{
                    text: '保存',
                    id: 'decoratesubmit',
                    iconCls: 'icon-ok',
                }, {
                    text: '关闭',
                    iconCls: 'icon-no',
                    handler: function () {
                        $("#decorateDlg").dialog('close');
                    }
                }]
            });
        } else {
            $.messager.alert('提示', '请先选择要编辑的记录！', 'warning');
        }
    };
    var dodecorate = function (rowId) {
        if (rowId) {
            let title = '装修模板';
            let url = '/admin/decorate/dodecorate?id=' + rowId;
            $('#rightTabs').tabs('close', title);
            addTab(title, url);
        } else {
            $.messager.alert('提示', '请先选择要装修的模板！', 'warning');
        }
    };
    var decorateRecycle = function (rowId) {
        $.messager.confirm('提示', '确定放回收站吗？', function (r) {
            if (r) {
                $.messager.progress();
                $.post('/admin/decorate/delete', {id: rowId}, function (data) {
                    $.messager.progress('close');
                    if (parseInt(data.ret) === 0) {
                        $.messager.show({
                            title: '提示',
                            msg: data.msg,
                            timeout: 3000,
                            showType: 'slide'
                        });
                        decorateReload();
                    } else {
                        $.messager.alert('提示', data.msg, 'warning');
                    }
                }, 'json');
            }
        });
    };
    var decorateDelete = function (rowId) {
        $.messager.confirm('提示', '确定彻底删除吗？', function (r) {
            if (r) {
                $.messager.progress();
                $.post('/admin/decorate/forceDelete', {id: rowId}, function (data) {
                    $.messager.progress('close');
                    if (parseInt(data.ret) === 0) {
                        $.messager.show({
                            title: '提示',
                            msg: data.msg,
                            timeout: 3000,
                            showType: 'slide'
                        });
                        decorateReload();
                    } else {
                        $.messager.alert('提示', data.msg, 'warning');
                    }
                }, 'json');
            }
        });
    };
    var decorateRestore = function (rowId) {
        $.messager.confirm('提示', '确定恢复数据吗？', function (r) {
            if (r) {
                $.messager.progress();
                $.post('/admin/decorate/restore', {id: rowId}, function (data) {
                    $.messager.progress('close');
                    if (parseInt(data.ret) === 0) {
                        $.messager.show({
                            title: '提示',
                            msg: data.msg,
                            timeout: 3000,
                            showType: 'slide'
                        });
                        decorateReload();
                    } else {
                        $.messager.alert('提示', data.msg, 'warning');
                    }
                }, 'json');
            }
        });
    };
    var decorateCopy = function (rowId) {
        $.messager.confirm('提示', '确定复制数据吗？', function (r) {
            if (r) {
                $.messager.progress();
                $.post('/admin/decorate/copy', {id: rowId}, function (data) {
                    $.messager.progress('close');
                    if (parseInt(data.ret) === 0) {
                        $.messager.show({
                            title: '提示',
                            msg: data.msg,
                            timeout: 3000,
                            showType: 'slide'
                        });
                        decorateReload();
                    } else {
                        $.messager.alert('提示', data.msg, 'warning');
                    }
                }, 'json');
            }
        });
    };
    var decorateDown = function (rowId) {
        $.messager.confirm('提示', '确定下架吗？', function (r) {
            if (r) {
                $.messager.progress();
                $.post('/admin/decorate/down', {id: rowId}, function (data) {
                    $.messager.progress('close');
                    if (parseInt(data.ret) === 0) {
                        $.messager.show({
                            title: '提示',
                            msg: data.msg,
                            timeout: 3000,
                            showType: 'slide'
                        });
                        decorateReload();
                    } else {
                        $.messager.alert('提示', data.msg, 'warning');
                    }
                }, 'json');
            }
        });
    };
    var decorateUp = function (rowId) {
        $.messager.confirm('提示', '确定上架吗？', function (r) {
            if (r) {
                $.messager.progress();
                $.post('/admin/decorate/up', {id: rowId}, function (data) {
                    $.messager.progress('close');
                    if (parseInt(data.ret) === 0) {
                        $.messager.show({
                            title: '提示',
                            msg: data.msg,
                            timeout: 3000,
                            showType: 'slide'
                        });
                        decorateReload();
                    } else {
                        $.messager.alert('提示', data.msg, 'warning');
                    }
                }, 'json');
            }
        });
    };
    var decorateReload = function () {
        $("#decorate").datagrid('reload');
    };
    var decorateSearch = function () {
        var height = $(window).height() - $(".top").height() - 46;
        var width = $(window).width() - $(".leftmenu").width() - 8;
        $("#decorate").datagrid({
            title: '模板列表',
            width: width,
            height: height,//高度
            singleSelect: true,//如果为true，则只允许选择一行
            striped: true,//是否显示斑马线效果。
            rownumbers: false,//如果为true，则显示一个行号列。
            pagination: true,//是否显示分页
            pageSize: 10,
            pageList: [10, 15, 20, 25],
            method: 'post',
            sortName: 'id',
            sortOrder: 'asc',
            idField: 'id',
            url: '/admin/decorate/index',
            queryParams: {
                type: 'shop'
            },
            fitColumns: true,//真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动。
            nowrap: true,//如果为true，则在同一行中显示数据。设置为true可以提高加载性能。 是否换行
            selectOnCheck: false,
            checkOnSelect: true,
            toolbar: '#tb<?php echo $uniqid;?>',
            columns: [[
                {field: 'id', title: 'ID', width: fixWidth(5)},
                {field: 'name', title: '名称', width: fixWidth(20), formatter: function (value, rowData, rowIndex) {
                        return value + (rowData.deleted_at ? '<span style="color: red">(已删除)</span>' : '');
                    }},

                {
                    field: 'type', title: '类型', width: fixWidth(5), formatter: function (value, rowData, rowIndex) {
                        switch (value) {
                            case 'shop':
                                return '商城';
                            case 'custom':
                                return '自定义';
                        }
                    }
                },
                {field: 'memo', title: '备注', width: fixWidth(10)},
                {
                    field: 'status', title: '状态', width: fixWidth(8), formatter: function (value, rowData, rowIndex) {
                        return value==='normal' ? '已发布' : '下架';
                    }, styler: function (value, rowData, rowIndex) {
                        return value==='normal' ? '' : 'color:red';
                    }
                },
                {field: 'created_at', title: '加入时间', width: fixWidth(10), sortable: true},
                {
                    field: 'decorate',
                    title: '装修',
                    width: fixWidth(20),
                    formatter: function (value, rowData, rowIndex) {
                        let buttons = '<div class="table"><span class="l-btn l-btn-small color9 table" onclick="dodecorate(\'' + rowData.id + '\')"><span class="l-btn-text">装修</span></span>';
                        if (rowData.status === 'hidden') {
                            buttons += '<span class="l-btn l-btn-small color9 table" onclick="decorateUp(\'' + rowData.id + '\')"><span class="l-btn-text">发布</span></span>';
                        }
                        if (rowData.status === 'normal') {
                            buttons += '<span class="l-btn l-btn-small color9 table" onclick="decorateDown(\'' + rowData.id + '\')"><span class="l-btn-text" style="color: red">下架</span></span>';
                        }
                        if(rowData.deleted_at){
                            buttons += '<span class="l-btn l-btn-small color9 table" onclick="decorateRestore(\'' + rowData.id + '\')"><span class="l-btn-text">恢复</span></span>';
                            buttons += '<span class="l-btn l-btn-small color9 table" onclick="decorateDelete(\'' + rowData.id + '\')"><span class="l-btn-text">彻底删除</span></span>';
                        }else {
                            buttons += '<span class="l-btn l-btn-small color9 table" onclick="decorateRecycle(\'' + rowData.id + '\')"><span class="l-btn-text">回收站</span></span>';
                        }
                        buttons += '<span class="l-btn l-btn-small color9 table" onclick="decorateCopy(\'' + rowData.id + '\')"><span class="l-btn-text">复制</span></span>';
                        return buttons + '</div>';

                    }
                },
            ]],
            onDblClickRow : function(index,field){
                decorateEdit();
            }
        });
    };
    onDatagridResize("#decorate");
    decorateSearch();
</script>
